# !/usr/bin/python3
# -*- coding:utf-8 -*-
# Copyright 2022 The Chinaunicom Software Team. All rights reserved.
# @FileName: sem_utils.py
# @Author : dyu
# @Time   : 2022-8-12

##################################################################################################
##                                       Utils & Tools                                          ##
##                                                                                              ##
##################################################################################################
import os, re, json, codecs
from tqdm import tqdm
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import numpy as np
from annoy_model import AnnoyRecall
from sem_predictor import SimbertEmbeding


def _write_txt(path, data, batch=True):
    """
    write txt utils
    :paramter path:
    :paramter dataset:
    """
    if batch:
        with codecs.open(path, mode='w', encoding='utf-8') as fw:
            for item in data:
                fw.write(item + '\n')
    else:
        with codecs.open(path, mode='a', encoding='utf-8') as fw:
            fw.write(data + '\n')

def _write_json(dataset,store_path):
    """
    write json utils
    :paramter store_path:  store file path
    :paramter dataset:
    """
    with codecs.open(store_path, mode='w', encoding='utf-8') as fw:
        fw.write(json.dumps(dataset, ensure_ascii=False))

def _read_json(path):
    """
    read json utils
    :paramter path:
    """
    with codecs.open(path, mode='r', encoding='utf-8') as fr:
        read_data = json.load(fr)
    return read_data

def _judge_point_zero(string):
    """
    Judge point and zero if in string
    :paramter string :
    """
    if string.isdigit():
        if ".0" in string:
            return string[:-2]
        else:
            return string


